<?php

/**
 * result actions.
 *
 * @package    question_answer
 * @subpackage result
 * @author     Your name here
 */
class resultActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {

    	$user_id = $this->getUser()->getGuardUser()->getId();
    $this->userss=$user_id;
  
	$c = new Criteria();	
	$c->addSelectColumn(QuestionPeer::BODY);
	$c->addSelectColumn(AnswerPeer::BODY);
	$c->addSelectColumn(ResultPeer::POINT);
	$c->addSelectColumn(ResultPeer::CHECK);
	$c->addSelectColumn(ResultPeer::ID);
	$c->add(ResultPeer::USER_ID, $user_id);	
	$c->addJoin(ResultPeer::QUESTION_ID,QuestionPeer::ID);
	$c->addJoin(ResultPeer::ANSWER_ID,ANSWERPeer::ID);	 	 		
	$this->resultss = ResultPeer::doSelectStmt($c);
        
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->result = ResultPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->result);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new resultForm();
    

  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new resultForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($result = ResultPeer::retrieveByPk($request->getParameter('id')), sprintf('Object result does not exist (%s).', $request->getParameter('id')));
    $this->form = new resultForm($result);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($result = ResultPeer::retrieveByPk($request->getParameter('id')), sprintf('Object result does not exist (%s).', $request->getParameter('id')));
    $this->form = new resultForm($result);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($result = ResultPeer::retrieveByPk($request->getParameter('id')), sprintf('Object result does not exist (%s).', $request->getParameter('id')));
    $result->delete();

    $this->redirect('result/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $result = $form->save();

      //$this->redirect('result/edit?id='.$result->getId());
      $this->redirect('answer/index');
    }
  }

 public function executeSubmit(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new resultForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

}
